package com.huatai.exchange.bootup.controller;

import com.alibaba.fastjson.JSON;
import com.faneui.bootup.controller.AbstractRestController;
import com.faneui.common.bean.FaneuiBaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.BeanMapper;
import com.faneui.common.utils.DateFormatUtils;
import com.faneui.common.utils.ListUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.huatai.exchange.dal.model.ExcCustDataFile;
import com.huatai.exchange.facade.CommunicationContents;
import com.huatai.exchange.facade.Contents;
import com.huatai.exchange.facade.CustDataFileController;
import com.huatai.exchange.facade.bean.CustDataFileVo;
import com.huatai.exchange.facade.bean.QueryCustDataFileVoListResp;
import com.huatai.exchange.integration.support.service.ScheduleBatchService;
import com.huatai.exchange.service.ExcCustDataFileService;
import com.huatai.exchange.service.ExcuteExchangeService;
import com.huatai.support.facade.bean.ScheduleBatchVo;
import com.huatai.support.facade.enums.TaskDealStatus;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "custdatafile")
@Api("客户数据文件配置")
public class CustDataFileControllerImpl extends AbstractRestController implements CustDataFileController {

    @Autowired
    private ExcCustDataFileService excCustDataFileService;

    @Autowired
    private ExcuteExchangeService excuteExchangeService;

    @Autowired
    private ScheduleBatchService scheduleBatchService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public QueryCustDataFileVoListResp queryCustDataFileVoList(@ModelAttribute CustDataFileVo custDataFileVo) throws Exception {
        QueryCustDataFileVoListResp resp = new QueryCustDataFileVoListResp();
        try {
            ExcCustDataFile param = BeanMapper.map(custDataFileVo, ExcCustDataFile.class);
            List<ExcCustDataFile> dataFiles = excCustDataFileService.queryExcCustDataFileList(param);
            if (ListUtils.isNotEmpty(dataFiles)) {
                String occurDate = DateFormatUtils.yyyyMMdd.format(new Date());
                List<CustDataFileVo> vos = Lists.newArrayList() ;
                for (ExcCustDataFile dataFile : dataFiles) {
                    CustDataFileVo vo = BeanMapper.map(dataFile, CustDataFileVo.class);
                    vo.setExcuteStatus(TaskDealStatus.W);
                    ScheduleBatchVo bVo = scheduleBatchService.queryScheduleBatchVo(occurDate, Contents.busin_cust_data_id + vo.getId());
                    if (bVo != null)
                        vo.setExcuteStatus(bVo.getDealStatus());
                    vos.add(vo) ;
                }
                resp.setCustDataFileVos(vos);
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/{custDataFileId}", method = RequestMethod.GET)
    @ResponseBody
    public FaneuiBaseResp executeCustDataFileExchange(@PathVariable String custDataFileId
            , @RequestParam(required = false) String occurDate) throws Exception {
        FaneuiBaseResp resp = new FaneuiBaseResp();
        if (StringUtils.isBlank(occurDate)) {
            occurDate = DateFormatUtils.yyyyMMdd.format(new Date());
        }
        try {
            ExcCustDataFile custDataFile = excCustDataFileService.queryExcCustDataFile(custDataFileId);
            String jsonProperties = custDataFile.getJsonProperties();
            Map<String, Object> resvMap = StringUtils.isBlank(jsonProperties) ? Maps.newHashMap() : JSON.parseObject(jsonProperties, HashMap.class);
            // 必填 地址

            resvMap.put(CommunicationContents.communication_dest, custDataFile);

            excuteExchangeService.executeExchangeDataFile(custDataFile.getExchangeInfoId(), occurDate, resvMap);

        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public FaneuiBaseResp modifyCustDataFileVo(@RequestBody CustDataFileVo custDataFileVo) throws Exception {
        FaneuiBaseResp resp = new FaneuiBaseResp();
        try {
            ExcCustDataFile excCustDataFile = BeanMapper.map(custDataFileVo, ExcCustDataFile.class);
            if(StringUtils.isBlank(excCustDataFile.getDestAddress())) {
                throw new BusinessException("目标邮件或地址不能为空") ;
            }

            excCustDataFileService.modifyCustDataFile(excCustDataFile);
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

}
